/*
 *  mini-SaaS
 *  Application for developing Software as a Service (SaaS) 
 *  Copyright (c) 2011 Anand Sudhakaran, Bryce Holton, Chao Zhang
 *  ASU
 *
 *  This file is part of mini-SaaS.
 *
 *  mini-SaaS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  mini-SaaS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with mini-SaaS; if not, see <http://www.gnu.org/licenses/>. 
 *
 */
package loginbean;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sql.*;
public class login extends HttpServlet{ 
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException,IOException{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  System.out.println("MySQL Connect Example.");
  Connection conn = null;
  DBManager db = new DBManager();
  String username="";
  String userpass="";
  String strQuery= ""; 
  Statement st=null;
  ResultSet rs=null;
  HttpSession session = request.getSession(true);
  try {
	  System.out.println("before classforname");
  conn = db.getDatabaseConnection();
  System.out.println("after classforname connection");
  if(request.getParameter("username")!=null &&
     request.getParameter("username")!="" && request.getParameter("password")!=null &&
     request.getParameter("password")!="")
  {
  username = request.getParameter("username").toString();
  userpass = request.getParameter("password").toString();
  strQuery="select * from mydb.TenantTable where" +
  		" TenantName='"+username+"' and  TenantPassword='"+userpass+"'";
  System.out.println(strQuery);
  st = conn.createStatement();
  rs = st.executeQuery(strQuery);
  int count=0;
  while(rs.next())
  {
  session.setAttribute("username",rs.getString(2));
  session.setAttribute("tenantid", rs.getString(1));
  count++;
  }
  if(count>0)
  {
  response.sendRedirect("welcome.jsp");
  }
  else
  {
  response.sendRedirect("login.jsp");
  }
  }
  System.out.println("Connected to the database"); 
  conn.close();
  System.out.println("Disconnected from database");
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
}